package stack

import (
	lls "github.com/emirpasic/gods/stacks/linkedliststack"
)

// https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/
// 用两个栈实现队列
// 时间复杂度: 插入: O(1); 删除: O(n); 查找: O(n)
type CQueue struct {
	dataStack *lls.Stack
	tempStack *lls.Stack
}

func Constructor() CQueue {
	queue := CQueue{lls.New(), lls.New()}
	return queue
}

func (this *CQueue) AppendTail(value int) {
	this.dataStack.Push(value)
}

func (this *CQueue) DeleteHead() int {
	if 0 == this.dataStack.Size() {
		return -1
	}
	for this.dataStack.Size() > 1 {
		currentValue, _ := this.dataStack.Pop()
		this.tempStack.Push(currentValue)
	}
	lastValue, _ := this.dataStack.Pop()
	returnValue := lastValue.(int)

	for {
		currentValue, ok := this.tempStack.Pop()
		if !ok {
			break
		}
		this.dataStack.Push(currentValue)
	}

	return returnValue
}

/**
 * Your CQueue object will be instantiated and called as such:
 * obj := Constructor();
 * obj.AppendTail(value);
 * param_2 := obj.DeleteHead();
 */
